package com.xmc.utils;

import com.alibaba.fastjson.JSON;
import com.xmc.pojo.Course;
import com.xmc.pojo.CourseMsg;
import com.xmc.pojo.User;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 谢孟呈
 */
public class BaseContext {

    public static User getCurrentUser(HttpServletRequest request){
        return (User) request.getSession().getAttribute("user");
    }

    public static String getCookie(){
        //第一次请求,获取到它的cookie
        String cookie = HttpClientUtils.getCookie("https://jwxt.gdupt.edu.cn/");
        return cookie;
    }

    public static void toLogin(User user,String cookie){
        Map<String,String> mapLogin = new HashMap<>();
        mapLogin.put("account",user.getCode());
        mapLogin.put("pwd",BaseContext.encodeBySunMisc(user.getPassword()));
        HttpClientUtils.sendPost2("https://jwxt.gdupt.edu.cn/login!doLogin.action", mapLogin, cookie);

    }


    public static List<CourseMsg> toCourseResult(String str, String stuCode){
        List<CourseMsg> courseMsgResultList = new ArrayList<>();

        List listJson = new ArrayList();
        List<? extends List> listAll = JSON.parseArray(str, listJson.getClass());
        //course
        List<Map<String,String>> list1 = listAll.get(0);
        List<Map<String,String>> list2 = listAll.get(1);
        System.out.println(list2);

        for (int i = 0; i < list1.size(); i++) {
            CourseMsg courseMsg = new CourseMsg();
            courseMsg.setCourseid(list1.get(i).get("kcbh"));
            courseMsg.setStudentid(stuCode);
            courseMsg.setTeachername(list1.get(i).get("teaxms"));
            courseMsg.setCoursename(list1.get(i).get("kcmc"));
            courseMsg.setClassplace(list1.get(i).get("jxcdmc"));
            courseMsg.setChapter(list1.get(i).get("sknrjj"));
            courseMsg.setType(list1.get(i).get("jcdm"));
            courseMsg.setHasCourseTime(list1.get(i).get("xq"));
            courseMsg.setZhouci(list1.get(i).get("zc"));
            for (int j = 0; j < list2.size(); j++) {
                if (courseMsg.getHasCourseTime().equals(list2.get(j).get("xqmc"))){
                    courseMsg.setClearTime(list2.get(j).get("rq"));
                }
            }
            courseMsgResultList.add(courseMsg);
        }

        return courseMsgResultList;

    }

    public static List<Course> CourseList(String cookie, String stuCode){
        List<Course> courseResultList = new ArrayList<>();
        //获取全部这个学期的课表
        String info = HttpClientUtils.sentGet("https://jwxt.gdupt.edu.cn/xsgrkbcx!getKbRq.action?xnxqdm=202201", cookie);
       //模拟请求体
        Map<String,String> mapGetCourse = new HashMap<>();
        mapGetCourse.put("xnxqdm","202201");
        mapGetCourse.put("page","1");
        mapGetCourse.put("rows","20");
        mapGetCourse.put("sort","kcbh");
        mapGetCourse.put("order","asc");
        //获取这个学期的全部课程
        String courseList = HttpClientUtils.sendPost("https://jwxt.gdupt.edu.cn/xskktzd!getDataList.action", mapGetCourse, cookie);
        Map<String,Object> mapJson = new HashMap();
        Map map = JSON.parseObject(courseList, mapJson.getClass());
        System.out.println(map);
        List<Map<String,String>> rows = (List<Map<String, String>>) map.get("rows");
        List listJson = new ArrayList();
        List<? extends List> listAll = JSON.parseArray(info, listJson.getClass());
        //解析course
        List<Map<String,String>> list1 = listAll.get(0);
        for (int i = 0; i < list1.size(); i++) {
            Course course = new Course();
            course.setCoursename(list1.get(i).get("kcmc"));
            course.setStudentid(stuCode);
            course.setCoursetype(list1.get(i).get("jxhjmc"));
            course.setCourseid(list1.get(i).get("kcbh"));
            course.setTeachername(list1.get(i).get("teaxms"));
            for (int i1 = 0; i1 < rows.size(); i1++) {
                if (course.getCourseid().equals(rows.get(i1).get("kcbh"))){
                    course.setCoursexf(rows.get(i1).get("xf"));
                }
            }
            courseResultList.add(course);

        }

        return courseResultList;

    }


    //base64解码
    public static String encodeBySunMisc(String str) {
        try {
            return new BASE64Encoder().encode(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    //获取行为
    public static String getAction(String url){
        int index = url.lastIndexOf("/");
        String substring = url.substring(index + 1);
        return substring;
    }
}
